//
//  XDGradeVideoController.m
//  XDEducation
//
//  Created by Ethan on 2017/5/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDGradeVideoController.h"
#import "CellLayout.h"
#define kCell @"cell"
#import "XDGradeVideoCell.h"
#import "HNVideoDetailController.h"
#import "HNVideoListModel.h"

@interface XDGradeVideoController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *classCollectionView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger Index;


@end

@implementation XDGradeVideoController

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.Index = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpSuperView];
}

- (void)setUpSuperView{
    
    CellLayout *layout = [CellLayout new];
    layout.lineCount = 2;
    layout.horizontalSpacing = 10;
    layout.verticalSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.classCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 15, kScreenWidth - 20, kScreenHeight - 123) collectionViewLayout:layout];
    self.classCollectionView.backgroundColor = [UIColor clearColor];
    self.classCollectionView.bounces = YES;
    self.classCollectionView.showsVerticalScrollIndicator = NO;
    self.classCollectionView.delegate = self;
    self.classCollectionView.dataSource = self;
    self.classCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        [self RefreshData];
    }];
    [self.classCollectionView.mj_footer beginRefreshing];
    [self.view addSubview:self.classCollectionView];
    [self.classCollectionView registerClass:[XDGradeVideoCell class] forCellWithReuseIdentifier:kCell];
    
}

#pragma mark collectionview delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XDGradeVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
    HNVideoListModel *Model = self.dataSource[indexPath.item];
    cell.Model = Model;
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGSize item = CGSizeMake(kScreenWidth/2 - 40 , kScreenHeight*300/1334.0);
    return item;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HNVideoDetailController *VC = [[HNVideoDetailController alloc] init];
    HNVideoListModel *ListModel = self.dataSource[indexPath.item];
    VC.ListModel = ListModel;
    [self presentViewController:VC animated:YES completion:nil];
    
}





#pragma mark - 网络请求
- (void)RefreshData
{
    [self GetUrlStrAndParamsWithComplete:^(NSString *UrlStr, NSDictionary *Params) {
        
        [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {
           
            [self EndRefresh];
            if ([responseObj[@"dataList"] isKindOfClass:[NSArray class]])
            {
                NSArray *Arr = (NSArray *)responseObj[@"dataList"];
                if (Arr.count == 0)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.classCollectionView.mj_footer endRefreshingWithNoMoreData];
                    });
                    
                }
                else
                {
                    for (NSDictionary *Dict in Arr)
                    {
                        [self.dataSource addObject:[[HNVideoListModel alloc] init_HNObjectWithDict:Dict]];
                    }
                    self.Index += 1;
                    [self.classCollectionView reloadData];
                }
            }
            else
            {
                [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
            }
        } failure:^(NSError *error) {
            [self EndRefresh];
            [MBProgressHUD showError:XDNetWorkError];
        }];
    
        
        
    }];
}

- (void)EndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.classCollectionView.mj_footer endRefreshing];
    });
}

/** 根据枚举得出url与params */
- (void)GetUrlStrAndParamsWithComplete:(void(^)(NSString *,NSDictionary *))Complete
{
    if (self.XDGradeVideoControllerType == HaoXiGuan)
    {
        Complete (@"media.do?action=videolist",@{@"vtype":@2,@"page":@(self.Index),@"type":@0});
    }
    else if (self.XDGradeVideoControllerType == HaoChengJi)
    {
        Complete (@"media.do?action=videolist",@{@"vtype":@0,@"page":@(self.Index),@"type":@0});
    }
    else if (self.XDGradeVideoControllerType == HaoSuYang)
    {
        Complete (@"media.do?action=videolist",@{@"vtype":@2,@"page":@(self.Index),@"type":@0});
    }
    else
    {
    }
}
@end
